<?php
class hero
{
	var $id;
	var $name;  //名称
	var $player; //玩家
	var $descript; //描述
	//笔记
	var $notes;
	//状态（hp,mp...)
	var $stat=array();
	//属性 
	var $attribute=array();
	//技能
	var $skills=array();
	//物品
	var $items=array();
	//装备（在身上的物品）
	var $gears=array();
	//日常任务
	var $tasks=array();
	//任务
	var $quests=array();
	//战斗
	var $battles=array();
	//坐标
	var $coord=array();
	//好友列表
	var $friends=array();
	
	
	function __construct($input_id=NULL)
	{
		if(isset($input_id))
		{
			$this->read_hero($input_id);
		}
		else
		{
			//$this->create_new_hero();
			//打消了这个想法。
		}
	}
	
	//新建英雄,初始化各项数据
	function create_new_hero($in_name,$in_player_id,$in_descript)
	{
		//这里没有设定name,id,descript,player,需要从参数中获取。
		$this->name=mysql_real_escape_string($in_name);
		$this->player=$in_player_id;
		$this->descript=mysql_real_escape_string($in_descript);
		$this->attribute=array(
							'str'=>1,
							'con'=>1,
							'int'=>1,
							'dex'=>1,
							'cha'=>1,
							'agi'=>1,
							'per'=>1,
							'wil'=>1
							);
		//出生点
		$this->coord=array(
							'x'=>1,
							'y'=>1
							);
		$this->skills=array('reading'=>1);
		$this->stat=array(
						'max_hp'=>5,
						'hp'=>5,
						'max_ep'=>5,
						'ep'=>5
						);
	}
	
	//从数据库读入一个英雄
	function read_hero($input_id)
	{
		$query_str="SELECT * FROM pcs WHERE id=$input_id";
		$sql_result=mysql_query($query_str,$GLOBALS['mysql']);
		if (mysql_num_rows($sql_result))
		{
			$this->attribute=json_decode(mysql_result($sql_result,0,'attribute'),true);
			$this->battles=json_decode(mysql_result($sql_result,0,'battles'),true);
			$this->friends=json_decode(mysql_result($sql_result,0,'friends'),true);
			$this->gears=json_decode(mysql_result($sql_result, n0,'gears'),true);
			$this->id=mysql_result($sql_result,0,'id');
			$this->items=json_decode(mysql_result($sql_result,0,'items'),true);
			$this->name=mysql_result($sql_result,0,'name');
			$this->notes=mysql_result($sql_result,0,'notes');
			$this->player=mysql_result($sql_result,0,'player');
			$this->quests=json_decode(mysql_result($sql_result,0,'quests'),true);
			$this->skills=json_decode(mysql_result($sql_result,0,'skills'),true);
			$this->stat=json_decode(mysql_result($sql_result,0,'stat'),true);
			$this->tasks=json_decode(mysql_result($sql_result,0,'tasks'),true);
		}
		else throw new Exception("$input_id 号角色没有找到。"); 
	}
	
	//将一个英雄存入数据库 
	function save_hero()
	{
		if (isset($this->id))
		{
			$query_str='UPDATE pcs SET
			attribute="'.mysql_real_escape_string(json_encode($this->attribute)).'",
			name="'.mysql_real_escape_string($this->name).'",
			battles="'.mysql_real_escape_string(json_encode($this->battles)).'",
			coord="'.mysql_real_escape_string(json_encode($this->coord)).'",
			descript="'.mysql_real_escape_string($this->descript).'",
			gears="'.mysql_real_escape_string(json_encode($this->gears)).'",
			items="'.mysql_real_escape_string(json_encode($this->items)).'",
			player="'.mysql_real_escape_string($this->player).'",
			quests="'.mysql_real_escape_string(json_encode($this->quests)).'",
			skills="'.mysql_real_escape_string(json_encode($this->skills)).'",
			stat="'.mysql_real_escape_string(json_encode($this->stat)).'",
			tasks="'.mysql_real_escape_string(json_encode($this->tasks)).'",
			friends="'.mysql_real_escape_string(json_encode($this->friends)).'",
			notes="'.mysql_real_escape_string(json_encode($this->notes)).'"
			WHERE id='.$this->id.'
			';
			mysql_query($query_str,$GLOBALS['mysql']);
			//echo $query_str;
		}
		else
		{
			$query_str='INSERT INTO pcs
			(attribute,name,battles,coord,descript,gears,items,player,quests,skills,stat,tasks,friends,notes)
			VALUES (
			"'.mysql_real_escape_string(json_encode($this->attribute)).'",
			"'.mysql_real_escape_string($this->name).'",
			"'.mysql_real_escape_string(json_encode($this->battles)).'",
			"'.mysql_real_escape_string(json_encode($this->coord)).'",
			"'.mysql_real_escape_string($this->descript).'",
			"'.mysql_real_escape_string(json_encode($this->gears)).'",
			"'.mysql_real_escape_string(json_encode($this->items)).'",
			"'.mysql_real_escape_string($this->player).'",
			"'.mysql_real_escape_string(json_encode($this->quests)).'",
			"'.mysql_real_escape_string(json_encode($this->skills)).'",
			"'.mysql_real_escape_string(json_encode($this->stat)).'",
			"'.mysql_real_escape_string(json_encode($this->tasks)).'",
			"'.mysql_real_escape_string(json_encode($this->friends)).'",
			"'.mysql_real_escape_string(json_encode($this->notes)).'"
			)';
			//echo $query_str;
			mysql_query($query_str,$GLOBALS['mysql']);
			$this->id=mysql_insert_id();
		}
	}
}
?>